package com.bjbn.controller;

import com.bjbn.service.CsvReaderService;
import com.bjbn.tasks.CsvProcessingTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/csv")
@Slf4j
public class CsvController {

    @Autowired
    private CsvProcessingTask csvProcessingTask;

    @Autowired
    private CsvReaderService csvReaderService;

    @PostMapping("/start")
    public ResponseEntity<Map<String, Object>> startContinuousProcessing() {
        log.info("Manual continuous processing start requested");
        
        Map<String, Object> response = new HashMap<>();
        
        try {
            csvProcessingTask.startContinuousProcessing();
            
            response.put("success", true);
            response.put("message", "Continuous processing started successfully");
            response.put("totalRecords", csvReaderService.getTotalRecords());
            response.put("isRunning", csvReaderService.isRunning());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("Error starting continuous processing: {}", e.getMessage(), e);
            
            response.put("success", false);
            response.put("message", "Error starting continuous processing: " + e.getMessage());
            
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @PostMapping("/stop")
    public ResponseEntity<Map<String, Object>> stopContinuousProcessing() {
        log.info("Manual continuous processing stop requested");
        
        Map<String, Object> response = new HashMap<>();
        
        try {
            csvProcessingTask.stopContinuousProcessing();
            
            response.put("success", true);
            response.put("message", "Continuous processing stopped successfully");
            response.put("isRunning", csvReaderService.isRunning());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("Error stopping continuous processing: {}", e.getMessage(), e);
            
            response.put("success", false);
            response.put("message", "Error stopping continuous processing: " + e.getMessage());
            
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @PostMapping("/reload")
    public ResponseEntity<Map<String, Object>> reloadData() {
        log.info("Manual data reload requested");
        
        Map<String, Object> response = new HashMap<>();
        
        try {
            csvProcessingTask.reloadData();
            
            response.put("success", true);
            response.put("message", "Data reloaded successfully");
            response.put("totalRecords", csvReaderService.getTotalRecords());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("Error reloading data: {}", e.getMessage(), e);
            
            response.put("success", false);
            response.put("message", "Error reloading data: " + e.getMessage());
            
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getStatus() {
        Map<String, Object> status = new HashMap<>();
        
        status.put("isRunning", csvReaderService.isRunning());
        status.put("totalRecords", csvReaderService.getTotalRecords());
        status.put("totalFiles", csvReaderService.getLoadedFiles().size());
        
        return ResponseEntity.ok(status);
    }

    @GetMapping("/status/detailed")
    public ResponseEntity<Map<String, Object>> getDetailedStatus() {
        return ResponseEntity.ok(csvReaderService.getDetailedStatus());
    }

    @GetMapping("/health")
    public ResponseEntity<Map<String, Object>> healthCheck() {
        Map<String, Object> health = new HashMap<>();
        health.put("status", "UP");
        health.put("service", "vehicle-data-processor");
        health.put("isRunning", csvReaderService.isRunning());
        health.put("totalRecords", csvReaderService.getTotalRecords());
        
        return ResponseEntity.ok(health);
    }
} 